<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 图标区 -->
            <div class="avater_logo">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 表单区 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="0px" class="login_form">
                <!-- 用户名  -->
                <el-form-item prop="username">
                    <el-input  placeholder="请输入账号" v-model="loginForm.username"  prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码"  v-model="loginForm.password" type="password" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <!-- 按钮区 -->
                <!-- 登录按钮  -->
                <el-form-item class="btns">
                    <el-button type="primary"  :plain="true"  @click="login">登录</el-button>
                    <el-button type="info" @click="resetFormInf">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 登录表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 验证规则
      rules: {
        // 用户名验证规则
        username: [
          { required: true, message: 'Please input your username !', trigger: 'blur' },
          { min: 3, max: 5, message: 'Bigger than 3 and Smaller than 10 !', trigger: 'blur' }
        ],
        // 密码验证规则
        password: [
          { required: true, message: 'Please input your password !', trigger: 'blur' },
          { min: 6, max: 16, message: 'Bigger than 6 and Smaller than 16 !', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetFormInf () {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async val => {
        // console.log(val)
        if (!val) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log(res)
        if (res.meta.status !== 200) {
          // this.$message.error(res.meta.msg)
          return this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        // 后面所有的访问都需要token 所以需要吧token保存到sessionStorage里面
        window.sessionStorage.setItem('token', res.data.token)
        // 跳转到主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
    height: 100%;
    background-color: #2b4b6b;
}
.login_box {
    height: 300px;
    width: 450px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    .avater_logo {
        width: 130px;
        height: 130px;
        border: #eee 1px  solid;
        border-radius: 50%;
        box-shadow: 0 0 10px #eee;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #ddd;
        }
    }
}
.btns {
    display: flex;
    justify-content: flex-end;
}
.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
